package com.yutel.gps;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

import com.yutel.gps.model.RunParameter;
import com.yutel.gps.server.Convert;

public class Main {
	public static void main(String[] args) {
		String exec = "java -jar gps4j.jar [-h] <-t table>";
		Options opt = null;
		HelpFormatter hf = null;
		try {
			opt = new Options();
			opt.addOption("h", false, "Lists short help");
			opt.addOption("t", "tableName", true, "table name,this is not null.");
			opt.addOption("x", "srcLngColumnName", true, "the src of lng column name,default gps_lng.");
			opt.addOption("y", "srcLatColumnName", true, "the src of lat column name,default gps_lat.");
			opt.addOption("m", "aimLngColumnName", true, "the aim of lng column name,default bd_lng.");
			opt.addOption("n", "aimLatColumnName", true, "the  aim of lat column name,default bd_lat.");
			opt.addOption("c", "statusColumnName", true, "status column name,default status.");
			opt.addOption("v", "statusValue", true, "status column value,default 0.");
			opt.addOption("s", "successValue", true, "status column success value,default 1.");
			opt.addOption("f", "frequency", true, "deal gps convert frequency,default 100ms.");
			hf = new HelpFormatter();
			CommandLineParser parser = new PosixParser();
			CommandLine cmd = parser.parse(opt, args);
			if (cmd.hasOption("h")) {
				hf.printHelp(exec, "", opt, "");
				return;
			}
			RunParameter rp = RunParameter.getIns();
			String table = cmd.getOptionValue("t");
			if (table != null) {
				rp.setTable(table);
			} else {
				hf.printHelp(exec, "table name of parameters is not null", opt, "");
				return;
			}
			String srcLngColumn = cmd.getOptionValue("x");
			if (srcLngColumn != null) {
				rp.setSrcLngColumn(srcLngColumn);
			}
			String srcLatColumn = cmd.getOptionValue("y");
			if (srcLatColumn != null) {
				rp.setSrcLatColumn(srcLatColumn);
			}
			String aimLngColumn = cmd.getOptionValue("m");
			if (aimLngColumn != null) {
				rp.setAimLngColumn(aimLngColumn);
			}
			String aimLatColumn = cmd.getOptionValue("n");
			if (aimLatColumn != null) {
				rp.setAimLatColumn(aimLatColumn);
			}
			String statusColumn = cmd.getOptionValue("c");
			if (statusColumn != null) {
				rp.setStatusColumn(statusColumn);
			}
			String statusValue = cmd.getOptionValue("v");
			if (statusValue != null) {
				rp.setStatusValue(Integer.parseInt(statusValue));
			}
			String successValue = cmd.getOptionValue("s");
			if (successValue != null) {
				rp.setSuccessValue(Integer.parseInt(successValue));
			}
			String frequency = cmd.getOptionValue("f");
			if (frequency != null) {
				rp.setFrequency(Long.parseLong(frequency));
			}
			Convert or = new Convert();
			or.start();
		} catch (ParseException e) {
			hf.printHelp(exec, "", opt, "");
		}
	}
}
